<template>
  <el-dialog :title="type === 'add' ? '新增密码' : '编辑密码'"
    :visible="dialogVisible"
    width="650px"
    @close="close">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-row>
        <el-col :span="24">
          <el-form-item label="网站名称" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="网站地址" prop="path">
            <el-input v-model="ruleForm.path"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="账号" prop="account">
            <el-input v-model="ruleForm.account"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password" show-password></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="类型" prop="type"> 
            <el-input v-model="ruleForm.type"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="reset('ruleForm')">取 消</el-button>
      <el-button v-if="type === 'add'" type="primary" @click="save('ruleForm')" :loading="loading">确 定</el-button>
      <el-button v-if="type === 'edit'" type="primary" @click="update('ruleForm')" :loading="loading">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      ruleForm: {
        name: '',
        path: '',
        account: '',
        password: '',
        type: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入网站名称', trigger: 'blur' }
        ],
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
      },
      loading: false,
      type: 'add',
    }
  },
  props: {
    dialogVisible: {
      type: Boolean
    },
    list: {
      type: Array
    }
  },
  methods: {
    close () {
      this.$refs.ruleForm.resetFields();
      this.$emit('update:dialogVisible', false)
    },
    open () {
      this.type = 'add'
      this.$emit('update:dialogVisible', true)
    },
    save(formName) { // 新增
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          const params = {
            name: this.ruleForm.name,
            path: this.ruleForm.path,
            account: this.ruleForm.account,
            password: this.ruleForm.password,
            type: this.ruleForm.type,
            uid: this.$Cookies.get('uid')
          }
          this.$API({
            name: 'addPassword',
            data: params,
            requireAuth: true
          }).then(res => {
            if (res.data.code === 200) {
              this.$message.success(res.data.msg)
              this.close()
              this.$emit('getAll')
            } else {
              this.$message.error(res.data.msg)
            }
          }).catch(error => {
            this.$message.error(error)
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
    reset(formName) {
      this.$refs[formName].resetFields();
      this.$emit('update:dialogVisible', false)
    },
    detaliInfo (id) { // 根据id获取详情
      this.type = 'edit'
      this.$emit('update:dialogVisible', true)
      this.$API({
        name: 'detailPassword',
        paths: [id],
        requireAuth: true
      }).then (res => {
        if (res.data.code === 200) {
          this.ruleForm = res.data.data
        } else {
          this.$message.error(res.data.msg)
        }
      }).catch(error => {
        this.$message.error(error)
      })
    },
    update(formName) { // 编辑
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$API({
            name: 'updatePassword',
            data: this.ruleForm,
            requireAuth: true
          }).then(res => {
            if (res.data.code === 200 && res.data.data[0] === 1) {
              this.$message.success(res.data.msg)
              this.close()
              this.$emit('getAll')
            } else {
              this.$message.error(res.data.msg)
            }
          }).catch(error => {
            this.$message.error(error)
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
  }
}
</script>

<style>

</style>